setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load documents with predicted topics
top3domText <- read.csv("textpool/top3domText.csv", stringsAsFactors = FALSE)

### prepare tasks
LIbroadcursory <- validateLabel(type = "LI", n = 500, 
                                text.predict = top3domText, text.name = "post_text",
                                labels = c("Working Class",
                                           "Planned Parenthood",
                                           "Farm Bill",
                                           "Economy",
                                           "Prescription Medicine",
                                           "Grants for Colleges",
                                           "Banking",
                                           "Government Spending",
                                           "Healthcare",
                                           "Debt Ceiling"), 
                                labels.index = c(1, 9, 21, 35, 44, 62, 65, 70, 88, 89), 
                                labels.add = c("Manufacturing",
                                               "\"Welcome Home\" Messages",
                                               "Islamic Extremists",
                                               "Military Affairs",
                                               "Foreign Affairs",
                                               "Military",
                                               "Military Service",
                                               "\"Thank you\" Messages",
                                               "Counter-terrorism",
                                               "Veterans"))

goldLI <- read.csv("goldstandard/goldLI.csv", stringsAsFactors = FALSE)
LIbroadcursory <- mixGold(tasks = LIbroadcursory, golds = goldLI)
LIbroadcursory <- recordTasks(type = "LI", tasks = LIbroadcursory,
                              path = "labelrecords/LIbroadcursory.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# LIbroadcursoryids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "LI",
#                                tasksrecord = LIbroadcursory, HITidspath = "HITids/LIbroadcursoryids1.Rdata")
# ### extend HITs if needed
# for(i in LIbroadcursoryids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/LIbroadcursoryids1.Rdata")
# LIbroadcursoryresults1 <- getResults(batch_id = "LIbroadcursory1", hit_ids = HITids, retry = FALSE)
# write.csv(LIbroadcursoryresults1, "LI/results/LIbroadcursoryresults1.csv", row.names = FALSE)
# evalResults(results = LIbroadcursoryresults1, key = LIbroadcursory, type = "LI")
# 
# ### send tasks (second trial)
# LIbroadcursoryids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "LI",
#                                tasksrecord = LIbroadcursory, HITidspath = "HITids/LIbroadcursoryids2.Rdata")
# ### extend HITs if needed
# for(i in LIbroadcursoryids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/LIbroadcursoryids2.Rdata")
# LIbroadcursoryresults2 <- getResults(batch_id = "LIbroadcursory2", hit_ids = HITids, retry = FALSE)
# write.csv(LIbroadcursoryresults2, "LI/results/LIbroadcursoryresults2.csv", row.names = FALSE)
# evalResults(results = LIbroadcursoryresults2, key = LIbroadcursory, type = "LI")